package validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import core.IEntityValidator;

public class PlayerValidator implements IEntityValidator {
	
	private String message = "";

	public boolean validate(String[] data) {
		// The massiv of data about new Player: String[] = {ownerId, firstname, lastname}

		if(data.length!=3) return false;
		
		// verification of the absence of empty fields in data
		for (int i = 0; i < data.length; i++) {
			if (!valLength(data[i])){
			message = "Fill in all required fields!";	
			return false;
			}
		}

		// pattern for name
		Pattern p_letters = Pattern.compile("[a-zA-Z]*|[à-ÿÀ-ß]*");
		// pattern for id
		Pattern p_id = Pattern.compile("[0-9]*");

		Matcher m_ownerId = p_id.matcher(data[0]);
		if (!m_ownerId.matches()){
			message = "Identificator of owner should be number!";
			return false;
			}
		
		Matcher m_firstname = p_letters.matcher(data[1]);
		if (!m_firstname.matches()){
			message = "Field 'Firstname' has an incorrect format!";
			return false;
		}
		
		Matcher m_lastname = p_letters.matcher(data[2]);
		if (!m_lastname.matches()){
			message = "Field 'Lastname' has an incorrect format!";
			return false;
		}
		
		message = "The operation completed successfully!";
		return true;
	}
	
	public String getMessage(){
		return message;
	}

	private boolean valLength(String data) {
		return data.length() > 0;
	}

}
